package com.spi.soconversion.base.definition;

import java.io.File;
import java.math.BigDecimal;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

import com.spi.soconversion.base.MarshallingException;
import com.spi.soconversion.orders.OpenSalesOrderDefinitionMarshaller;

public class OpenOrderDefinitionMarshallerTest {

	public static void main(String[] args) {	
		System.out.println("Creating Open Sales Order definition...");

		// create open sale order definition
		OpenSalesOrderDefinition orderDef = new OpenSalesOrderDefinition();
		orderDef.setBillToID("Test");
		orderDef.setDocumentType("Test1");
		
		OrderExtension extn = new OrderExtension();
		extn.setExtnIsDepositAmountCollected("test");
		
		OrderLine line = new OrderLine();
		line.setDeliveryMethod("delivery");
						
		OrderLineExtension ole = new OrderLineExtension();
		ole.setExtnAmountCollected("100");
		
		OrderLineOption olo = new OrderLineOption();
		olo.setOptionDescription("test");
		line.addOrderLineOption(olo);
		
		LineCharge lc = new LineCharge();
		lc.setChargeCategory("t");
		line.addLineCharge(lc);
		
		LineTax lt = new LineTax();
		lt.setTax(new BigDecimal("0.00"));
		line.addLineTax(lt);
		
		orderDef.addOrderLine(line);
		
		HeaderCharge hc = new HeaderCharge();
		hc.setChargeAmount(new BigDecimal("100"));
		orderDef.addHeaderCharge(hc);
		
		HeaderTax ht = new HeaderTax();
		ht.setTax(new BigDecimal("100"));
		orderDef.addHeaderTax(ht);
		
		// marshall the created open sale order definition
		OpenSalesOrderDefinitionMarshaller marshaller = OpenSalesOrderDefinitionMarshaller.getInstance();
		try {
			marshaller.createOrderDefinitionWriter(orderDef);
			Document document = marshaller.writeOrderDefinition(orderDef);
			writeXmlFile(document, "OpenSalesOrder.xml");
			System.out.println("A New Open Sales Order definition has been created.");			
		} catch (MarshallingException e) {
			e.printStackTrace();
		}						
	}
	
	public static void writeXmlFile(Document doc, String filename) {
        
        try {
            // Prepare the DOM document for writing
            Source source = new DOMSource(doc);

            // Prepare the output file
            File file = new File(filename);
            Result result = new StreamResult(file);

            // Write the DOM document to the file
            Transformer xformer = TransformerFactory.newInstance().newTransformer();
            xformer.transform(source, result);

        } catch (TransformerConfigurationException e) {
                e.printStackTrace();
        } catch (TransformerException e) {
                e.printStackTrace();
        } catch (Exception e) {
                e.printStackTrace();
    }
}		
}
